//从yyyy-MM-ddZHH:mm:ssT提取HH:mm
export const getDate = (dateString) => {
  const date = new Date(dateString)
  const hours = date.getUTCHours().toString().padStart(2, '0')
  const minutes = date.getUTCMinutes().toString().padStart(2, '0')
  const time = `${hours}:${minutes}`
  return time
}

//进行排序（时间越晚越靠前）
export const getTime1 = (timeArray) => {
  return timeArray.sort(dateData('time', false))
}

//将数据按照time属性的yyyy-MM-dd来进行分组
export const groupSortByDate = (dataArray) => {
  let group_data = {}
  //遍历数组
  dataArray.forEach((temp_data) => {
    let temp_date = temp_data.time.substring(0, 13) //提取

    if (group_data.hasOwnProperty(temp_date)) {
      //加入
      group_data[temp_date].push(temp_data)
    } else {
      group_data[temp_date] = [temp_data]
    }
  })
  return Object.values(group_data) //再提取
}

//根据HH:mm对时间进行排序(限制传入数组有子属性time！)
export const sortHourAndMinute = (data) => {
  data.sort((a, b) => {
    const timeA = a.time
    const timeB = b.time

    // 比较时间字符串
    if (timeA < timeB) return -1
    if (timeA > timeB) return 1
    return 0
  })
  return data
}

//bol为true为升序，bol为false为降序
function dateData(property, bol) {
  return function (a, b) {
    var value1 = a[property]
    var value2 = b[property]
    if (bol) {
      // 升序
      return Date.parse(value1) - Date.parse(value2)
    } else {
      // 降序
      return Date.parse(value2) - Date.parse(value1)
    }
  }
}
